package utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;

public final class ObjectUtils {

    
    public final static byte[] toBytes(Object obj)
            throws IOException {
        
        ByteArrayOutputStream bos = null;
        ObjectOutputStream    oos = null;

        try {
            
            bos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(bos);

            oos.writeObject(obj);
            oos.flush();

            byte [] bytesObject = bos.toByteArray();

            oos.close();
            bos.close();

            return bytesObject;

        } catch (IOException ex) {
            try {
                if (bos != null) {
                    bos.close();
                }
            } catch (IOException e) {}

            try {
                if (oos != null) {
                    oos.close();
                }
            } catch (IOException e) {}
            
            throw  new IOException(ex);
        }
    }


    public final static Object toObject(byte[] bytes)
            throws IOException, ClassNotFoundException{
        
        ByteArrayInputStream byteArrayInputStream = null;
        ObjectInputStream objectInputStream = null;
        Object object = null;

        try {

            byteArrayInputStream = new ByteArrayInputStream(bytes);
            objectInputStream = new ObjectInputStream(byteArrayInputStream);
            
            object = objectInputStream.readObject();

            return object;

        } catch (IOException e) {

            if(byteArrayInputStream!=null)
                try {
                 byteArrayInputStream.close();
                } catch (IOException ex) {}

            if(objectInputStream!=null)
                try {
                 objectInputStream.close();
                } catch (IOException ex) {}

            throw  new IOException(e);
        } catch (ClassNotFoundException e) {

            if(byteArrayInputStream!=null)
                try {
                 byteArrayInputStream.close();
                } catch (IOException ex) {}
            
             if(objectInputStream!=null)
                try {
                 objectInputStream.close();
                } catch (IOException ex) {}
            
            throw  new ClassNotFoundException(e.getMessage());
        }      
    }


    public final static Object load(String filename)
            throws ClassNotFoundException, IOException {

        ObjectInputStream objectInput = null;
        FileInputStream   fileInput   = null;

        try {
            
            fileInput   = new FileInputStream(filename);
            objectInput = new ObjectInputStream(fileInput);

            Object object = (Object) objectInput.readObject();

            fileInput.close();
            objectInput.close();

            return object;
            
        } catch (ClassNotFoundException e) {
            try {
                if (objectInput != null) {
                    objectInput.close();
                }
            } catch (IOException e1) {}

            try {
                if (fileInput != null) {
                    fileInput.close();
                }
            } catch (IOException e1) {}
            throw new ClassNotFoundException(e.getMessage());
        } catch (IOException e) {
            try {
                if (objectInput != null) {
                    objectInput.close();
                }
            } catch (IOException e1) {}

            try {
                if (fileInput != null) {
                    fileInput.close();
                }
            } catch (IOException e1) {}
            throw new IOException(e);
        }
    }

    
    public final static void store(Object object, String filename)
            throws IOException {

        ObjectOutput     output     = null;
        FileOutputStream fileOutput = null;

        try {
            fileOutput = new FileOutputStream(filename);
            output     = new ObjectOutputStream(fileOutput);

            output.writeObject(object);
            output.flush();

            output.close();
            fileOutput.close();

        } catch (IOException e) {
            try {
                if (output != null) {
                    output.close();
                }
            } catch (IOException e1) {}

            try {
                if (fileOutput != null) {
                    fileOutput.close();
                }
            } catch (IOException e1) {}
            throw new IOException(e);
        }
    }

}

